﻿using SQLDeploy.Data.Internal;
using SQLDeploy.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Xml.Serialization;


namespace SQLDeploy.Data.Deployment.Steps.Tasks
{
    public abstract class _BaseTask : ValidationNotifier, ITask
    {
        private Step _parentStep;
        private string _name;
        private string _connectionName;
        private bool _ReplicatedOnly = false;

        public _BaseTask(Step parentStep) 
        {
            Check.notNull(parentStep, "ParentStep");
            _parentStep = parentStep;
        }

        public _BaseTask(Step parentStep, string name, string connectionName)
        {
            Check.notNull(parentStep, "ParentStep");
            Check.notNullOrEmpty(name, "Name");
            Check.notNullOrEmpty(connectionName, "connectionName");
            _parentStep = parentStep;
            Name = name;
            ConnectionName = connectionName;
        }

        /// <summary>
        /// the Prefix that will be used when loggin information on this task.
        /// </summary>
        [XmlIgnore]
        public string TaskNameHeader
        {
            get
            {
                return string.Format("Step [Id: {0} - {1}] Task [type: [{3} - name: {4}] - ",
                    new object[] { ParentStep.Id, ParentStep.Name, TaskType, Name });
            }
        }

        [XmlIgnore]
        public Step ParentStep
        {
            get { return _parentStep; }
        }


        /// <summary>
        /// The name of the task.
        /// </summary>
        [XmlAttribute]
        public string Name
        {
            get { return _name; }
            set
            {
                Check.notNullOrEmpty(value, "Name");
                if (_name == null || !Internal.Shared.DefaultStringComparer().Equals(_name, value))
                {
                    _name = value;
                    OnPropertyChanged("Name");
                }
            }
        }

        /// <summary>
        /// The name of the Connection that this task should be executed against
        /// </summary>
        [XmlAttribute]
        public string ConnectionName
        {
            get { return _connectionName; }
            set
            {
                Check.notNullOrEmpty(value, "ConnectionName");
                if (_connectionName == null ||
                    !Internal.Shared.DefaultStringComparer().Equals(_connectionName, value))
                {
                    _connectionName = value;
                    OnPropertyChanged("ConnectionName");
                }

            }
        }

        /// <summary>
        /// When ReplicatedOnly is set to true this task will only be processed when executinged a "Replicated" Environment.
        /// </summary>
        [XmlAttribute]
        public bool ReplicatedOnly
        {
            get { return _ReplicatedOnly; }
            set
            {
                if (_ReplicatedOnly != value)
                {
                    _ReplicatedOnly = value;
                    OnPropertyChanged("ReplicatedOnly");
                }
            }
        }

        public string TaskType
        {
            get { return GetType().Name;  }
        }
        
        

    }
}
